package com.gowants.pems.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gowants.pems.mapper.SysDictMapper;
import com.gowants.pems.model.SysDict;
import com.gowants.pems.util.DateUtil;
import com.gowants.pems.util.RedisUtils;

@Service(value = "ScmsDictService")
public class SysDictService extends NewBaseService<SysDict> {

	@Autowired
	private SysDictMapper ScmsDictMapper;
	@Autowired
	private RedisUtils redisUtils;
	/**
	 * key 前缀，防止和其他地方的key可以冲突
	 */
	@Value("${gowants.redisKey}")
	private String redisKey;
	@Override
	public int save(SysDict record) {
		record.setId(UUID.randomUUID().toString().replace("-", ""));
		record.setCreateTime(DateUtil.getDate());
		int i = ScmsDictMapper.save(record);
		if (i > 0) {
			String key = redisKey+"groupId:" + record.getGroupId();
			// 先取出缓存中是否存在
			String spObject = redisUtils.getStringRedis(key);
			if (null != spObject) {
				redisUtils.delStringRedis(key);
			}
			SysDict record0 = new SysDict();
			record0.setGroupId(record.getGroupId());
			List<SysDict> list1 = ScmsDictMapper.select(record0);
			Random r = new Random();
			redisUtils.setStringRedis(redisKey+"groupId:" + record0.getGroupId(), JSON.toJSON(list1).toString(),
					r.nextInt(999999));
		}
		return i;
	}

	@Override
	public int delete(String primaryKey) {
		SysDict record = ScmsDictMapper.selectOne(primaryKey);
		int i = ScmsDictMapper.delete(primaryKey);
		if (i > 0) {
			String key = redisKey+"groupId:" + record.getGroupId();
			// 先取出缓存中是否存在
			String spObject = redisUtils.getStringRedis(key);
			if (null != spObject) {
				redisUtils.delStringRedis(key);
			}
			SysDict record0 = new SysDict();
			record0.setGroupId(record.getGroupId());
			List<SysDict> list1 = ScmsDictMapper.select(record0);
			Random r = new Random();
			redisUtils.setStringRedis(redisKey+"groupId:" + record0.getGroupId(), JSON.toJSON(list1).toString(),
					r.nextInt(999999));
		}
		return i;
	}

	@Override
	public int update(SysDict record) {
		int i = ScmsDictMapper.update(record);
		if (i > 0) {
			String key = redisKey+"groupId:" + record.getGroupId();
			// 先取出缓存中是否存在
			String spObject = redisUtils.getStringRedis(key);
			if (null != spObject) {
				redisUtils.delStringRedis(key);
			}
			SysDict record0 = new SysDict();
			record0.setGroupId(record.getGroupId());
			List<SysDict> list1 = ScmsDictMapper.select(record0);
			Random r = new Random();
			redisUtils.setStringRedis(redisKey+"groupId:" + record0.getGroupId(), JSON.toJSON(list1).toString(),
					r.nextInt(999999));
		}
		return i;
	}

	@Override
	public SysDict selectOne(String primaryKey) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<SysDict> select(SysDict record) {
		return ScmsDictMapper.select(record);
	}

	public List<SysDict> selectList(SysDict record) {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(record.getGroupId())) {
			String key = redisKey+"groupId:" + record.getGroupId();
			// 先取出缓存中是否存在
			List<SysDict> list = new ArrayList<SysDict>();
			String spObject = redisUtils.getStringRedis(key);
			if (null != spObject) {
				if (StringUtils.isNotBlank(spObject)) {
					list = JSONObject.parseArray(spObject, SysDict.class);
					if (null != record.getDictId()) {
						List<SysDict> list0 = new ArrayList<SysDict>();
						for (SysDict sd : list) {
							if (sd.getDictId() == record.getDictId()) {
								list0.add(sd);
							}
						}
						return list0;
					} else {
						return list;
					}
				}
			} else {

				if (null == record.getDictId()) {
					List<SysDict> list1 = ScmsDictMapper.select(record);
					Random r = new Random();
					redisUtils.setStringRedis(redisKey+"groupId:" + record.getGroupId(), JSON.toJSON(list1).toString(),
							r.nextInt(999999));
					return list1;
				} else {
					SysDict record0 = new SysDict();
					record0.setGroupId(record.getGroupId());
					List<SysDict> list1 = ScmsDictMapper.select(record0);
					Random r = new Random();
					redisUtils.setStringRedis(redisKey+"groupId:" + record.getGroupId(), JSON.toJSON(list1).toString(),
							r.nextInt(999999));
					List<SysDict> list0 = new ArrayList<SysDict>();
					for (SysDict sd : list1) {
						if (sd.getDictId() == record.getDictId()) {
							list0.add(sd);
						}
					}
					return list0;
				}

			}

		}

		return ScmsDictMapper.select(record);
	}

	public List<SysDict> selectNew(SysDict record) {
		// TODO Auto-generated method stub
		return ScmsDictMapper.selectNew(record);
	}

	@Override
	public int getCount(SysDict record) {
		// TODO Auto-generated method stub
		return 0;
	}

	/**
	 * <添加时，校验 groupId+dictId是否重复>
	 * 
	 * @param record
	 * @return
	 * @remake create by TPH 2020年4月9日 下午4:50:56
	 */
	public int saveCheck(SysDict record) {
		return ScmsDictMapper.saveCheck(record);
	}

	/**
	 * <更新时，校验 groupId+dictId是否重复>
	 * 
	 * @param record
	 * @return
	 * @remake create by TPH 2020年4月9日 下午4:58:41
	 */
	public int updateCheck(SysDict record) {
		return ScmsDictMapper.updateCheck(record);
	}

	public String getDictName(Map<String, Object> map) {
		return ScmsDictMapper.getDictName(map);
	}
}
